<?php

if (!defined('BASEPATH'))
    exit('Tidak Diperkenankan mengakses langsung');
/* Class  Model : historibayar
 * di Buat oleh Diar PHP Generator
 * Update List untuk grid karena program generatorku lom sempurna ya hehehehehe */

class modelhistoribayar extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function getArrayListhistoribayar() { /* spertinya perlu lock table */
        $xBuffResul = array();
        $xStr = "SELECT " .
                "idx," .
                "idcustomer," .
                "idpaket," .
                "jenisbayar," .
                "total," .
                "date_bayar," .
                "statusbayar," .
                "idpegawai" .
                " FROM historibayar   order by idx ASC ";
        $query = $this->db->query($xStr);
        foreach ($query->result() as $row) {
            $xBuffResul[$row->idx] = $row->idcustomer;
        }
        return $xBuffResul;
    }

    function getListhistoribayar($xAwal, $xLimit, $xSearch = '') {
        if (!empty($xSearch)) {
            $xSearch = "Where idcustomer like '%" . $xSearch . "%'";
        }
        $xStr = "SELECT " .
                "idx," .
                "idcustomer," .
                "idpaket," .
                "jenisbayar," .
                "total," .
                "date_bayar," .
                "statusbayar," .
                "idpegawai" .
                " FROM historibayar $xSearch order by idx DESC limit " . $xAwal . "," . $xLimit;
        $query = $this->db->query($xStr);
        return $query;
    }
    function getListhistoribayarbydate($xAwal, $xLimit, $xSearch = '',$date_mulai='NOW()',$date_akhir='NOW()') {
        
            $xSearch = "Where (idcustomer in (SELECT idx FROM customer WHERE CONCAT_WS(' ',firstname,midlename,lastname) like '%" . $xSearch . "%' AND status='1' ))" .
                       "  AND (date_bayar >= '".$date_mulai."' AND date_bayar <= '".$date_akhir."') "; 
        $xStr = "SELECT " .
                "idx," .
                "idcustomer," .
                "idpaket," .
                "jenisbayar," .
                "total," .
                "date_bayar," .
                "statusbayar," .
                "idpegawai" .
                " FROM historibayar $xSearch  order by idx DESC limit " . $xAwal . "," . $xLimit;
        //print_r($xStr);
        $query = $this->db->query($xStr);
        return $query;
    }

    function getDetailhistoribayar($xidx) {
        $xStr = "SELECT " .
                "idx," .
                "idcustomer," .
                "idpaket," .
                "jenisbayar," .
                "total," .
                "date_bayar," .
                "statusbayar," .
                "idpegawai" .
                " FROM historibayar  WHERE idx = '" . $xidx . "'";

        $query = $this->db->query($xStr);
        $row = $query->row();
        return $row;
    }

    function getDetailhistoribayarbyid($xidcustomer) {
        $xStr = "SELECT " .
                "idx," .
                "idcustomer," .
                "idpaket," .
                "jenisbayar," .
                "total," .
                "date_bayar," .
                "statusbayar," .
                "idpegawai" .
                " FROM historibayar  WHERE  idcustomer = '" . $xidcustomer . "' order by idx DESC limit 1";

        $query = $this->db->query($xStr);
        $row = $query->row();
        return $row;
    }
    
    function getLastIndexhistoribayar() { /* spertinya perlu lock table */
        $xStr = "SELECT " .
                "idx," .
                "idcustomer," .
                "idpaket," .
                "jenisbayar," .
                "total," .
                "date_bayar," .
                "statusbayar," .
                "idpegawai" .
                " FROM historibayar order by idx DESC limit 1 ";
        $query = $this->db->query($xStr);
        $row = $query->row();
        return $row;
    }

    Function setInserthistoribayarregister($xidx, $xidcustomer, $xidpaket, $xjenisbayar, $xtotal, $xdate_bayar,$xstatusbayar='N', $xidpegawai) {
        $this->setDeletehistoribayarbyid($xidcustomer,$xidpaket); 
        $xStr = " INSERT INTO historibayar( " .
                "idx," .
                "idcustomer," .
                "idpaket," .
                "jenisbayar," .
                "total," .
                "date_bayar," .
                "statusbayar," .
                "idpegawai) VALUES('" . $xidx . "','" . $xidcustomer . "','" . $xidpaket . "','" . $xjenisbayar . "','" . $xtotal . "',CURDATE(),'" . $xstatusbayar . "','" . $xidpegawai . "')";
        $query = $this->db->query($xStr);
        return $xidx;
    }
    Function setInserthistoribayar($xidx, $xidcustomer, $xidpaket, $xjenisbayar, $xtotal, $xdate_bayar,$xstatusbayar='N', $xidpegawai) {
        $this->setDeletehistoribayarbyid($xidcustomer,$xidpaket); 
        $xStr = " INSERT INTO historibayar( " .
                "idx," .
                "idcustomer," .
                "idpaket," .
                "jenisbayar," .
                "total," .
                "date_bayar," .
                "statusbayar," .
                "idpegawai) VALUES('" . $xidx . "','" . $xidcustomer . "','" . $xidpaket . "','" . $xjenisbayar . "','" . $xtotal . "','" . $xdate_bayar . "','" . $xstatusbayar . "','" . $xidpegawai . "')";
        $query = $this->db->query($xStr);
        return $xidx;
    }

    Function setUpdatehistoribayar($xidx, $xidcustomer, $xidpaket, $xjenisbayar, $xtotal, $xdate_bayar,$xstatusbayar, $xidpegawai) {
        $xStr = " UPDATE historibayar SET " .
                "idx='" . $xidx . "'," .
                "idcustomer='" . $xidcustomer . "'," .
                "idpaket='" . $xidpaket . "'," .
                "jenisbayar='" . $xjenisbayar . "'," .
                "total='" . $xtotal . "'," .
                "date_bayar='" . $xdate_bayar . "'," .
                "statusbayar='" . $xstatusbayar . "'," .
                "idpegawai='" . $xidpegawai . "' WHERE idx = '" . $xidx . "'";
        $query = $this->db->query($xStr);
        return $xidx;
    }

    function setDeletehistoribayar($xidx) {
        $xStr = " DELETE FROM historibayar WHERE historibayar.idx = '" . $xidx . "'";

        $query = $this->db->query($xStr);
        $this->setInsertLogDeletehistoribayar($xidx);
    }

   function setDeletehistoribayarbyid($xidcustomer,$xidpaket) {
        $xStr = " DELETE FROM historibayar WHERE  idcustomer = '" . $xidcustomer . "' and idpaket = '".$xidpaket."'";

        $query = $this->db->query($xStr);
        
    }

    function setInsertLogDeletehistoribayar($xidx) {
        $xidpegawai = $this->session->userdata('idpegawai');
        $xStr = "insert into logdelrecord(idxhapus,nmtable,tgllog,ideksekusi) values($xidx,'historibayar',now(),$xidpegawai)";
        $query = $this->db->query($xStr);
    }

}

?>